package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.beanResult.SearchByNameResult;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.SearchByNameModel;
import com.smalls.newvideotwo.mvp.model.modelInterface.ISearchByNameModel;
import com.smalls.newvideotwo.mvp.view.ISearchByNameView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;


public class SearchByNamePresenter extends BasePresenter<ISearchByNameView> implements BaseBeanResult.ISearchByNameResult {
    private ISearchByNameModel model = new SearchByNameModel();
    private String searchStr;
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "SearchByNamePresenter":"SearchByNamePresenter";
    public SearchByNamePresenter(ISearchByNameView view) {
        super(view);
    }

    public void searchByName(String searchByNameBean,String programType) {
        this.model.searchByName(searchByNameBean,programType,this);
        this.searchStr = searchByNameBean;
    }

    public void onNext(SearchByNameResult bean) {
        if (bean == null || bean.getmAlbumList() == null || bean.getmAlbumList().size() == 0) {
            LogUtil.e(TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((ISearchByNameView) this.view).getSearchByNameFail(PortalErr.PORTAL_ROOT_COLUMN);
            return;
        }
        LogUtil.e(TAG, "获取数据成功 searchStr="+bean.getSearchStr());
        ((ISearchByNameView) this.view).getSearchByNameSuc(bean, bean.getSearchStr());
    }

    public void onError(Throwable e) {
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)) {
            LogUtil.e(TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;
        } else {
            LogUtil.e(TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((ISearchByNameView) this.view).getSearchByNameFail(errCode);
    }

    public SearchByNameModel getModel() {
        return (SearchByNameModel) this.model;
    }
}
